import { httpClient } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { PhoneDto, VerifyCodeCheckDto, VerifyCodeConfirmDto } from './model';
import { AjaxResult } from '../model';

/** 短信模块URL枚举 */
enum Api {
  /** 读取验证码短信记录列表 */
  ReadCaptchaRecords = '/api/sms/readCaptchaRecords',

  /** 发送短信验证码 */
  SendVerifyCode = '/api/sms/sendVerifyCode',

  /** 校验短信验证码，只作为前端Ajax验证，验证成功不确认验证码，验证码仍需传到后端进行再次验证 */
  CheckVerifyCode = '/api/sms/checkVerifyCode',
  /** 验证短信验证码 */
  ConfirmVerifyCode = '/api/sms/confirmVerifyCode',
}

/**
 * 发送短信验证码
 * @param dto 手机号码DTO
 * @param mode 错误消息模型
 */
export function sendSmsVerifyCode(dto: PhoneDto, mode: ErrorMessageMode = 'message') {
  return httpClient.post<AjaxResult>(
    {
      url: Api.SendVerifyCode,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 校验短信验证码，只作为前端Ajax验证，验证成功不确认验证码，验证码仍需传到后端进行再次验证
 * @param dto 短信验证码校验DTO
 * @param mode 错误消息模型
 */
export function checkSmsVerifyCode(dto: VerifyCodeCheckDto, mode: ErrorMessageMode = 'none') {
  return httpClient.post<boolean>(
    {
      url: Api.CheckVerifyCode,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * 确认短信验证码
 * @param dto 短信验证码确认DTO
 * @param mode 错误消息模型
 */
export function confirmSmsVerifyCode(
  dto: VerifyCodeConfirmDto,
  mode: ErrorMessageMode = 'message',
) {
  return httpClient.post<boolean>(
    {
      url: Api.ConfirmVerifyCode,
      params: dto,
    },
    {
      errorMessageMode: mode,
    },
  );
}
